﻿<!DOCTYPE html>
<html>
<head>
    <title>CGI</title>
</head>
<body>
    <h1>Our Page</h1>
    <script>
        //01
        //we create a blank object task
        //then we calling Object.defineProperty
        //1st argument is object that we going to define a property on
        //2nd argument is the name of the property
        //3rd argument is Object and this is the configuration object, now value for the property

        var task = {};
        Object.defineProperty(task, 'text', {
            value: 'hello CGI!'
        });
        console.log(task.text);


        //02
        //property here is writable by setting writable flag to true, comment out //writable: true
        /*
        'use strict';
        var task = {};
        Object.defineProperty(task, 'text', {
            value: 'Get this job done!',
            writable: true
        });
        task.text = task.text + '...NOW!';
        console.log(task.text);
        */

        //03
        /*
        'use strict';
        var task = {};
        Object.defineProperty(task, 'text', {
            value: 'Get this job done!',
            //enumerable: true
        });
        for (var f in task)
            console.log(f);
        */

        //04
        //to redefine need to set  configurable: true
        /*
        'use strict';
        var task = {};
        Object.defineProperty(task, 'text', {
            value: 'Get this job done!',
            configurable: false
            //configurable: true
        });
        Object.defineProperty(task, 'text', {
            value: 'Done!'
        });
        console.log(task.text);
        */

        //05
        //defineProperty getter function
        //useful for calculation,etc
        /*
        'use strict';
        var task = {
            _dueDate: '1/15/16'
        };
        Object.defineProperty(task, 'dueDate', {
            get: function () {
                return this._dueDate;
            }
        });

        console.log(task.dueDate);
        */
        //06
        //defineProperty getter & setter function
        /*
        'use strict';
        var task = {
            _dueDate: '1/15/16'
        };
        Object.defineProperty(task, 'dueDate', {
            get: function () {
                return this._dueDate;
            },
            set: function (newValue) {
                this._dueDate = newValue;
            }
        });

        task.dueDate = '2/2/16';
        console.log(task.dueDate);
        */

        //07
        //multiple properties
        /*
        'use strict';
        var task = {};
        Object.defineProperties(task, {
            'text': {
                value: 'New Task'
            },
            'dueDate': {
                value: '1/15/16'
            }
        });
        console.log(task.text + ' Due:' + task.dueDate);
        */

        //08
        //to know all the object and their property configurations
        //descriptor object
        /*
        'use strict';
        var task = {};
        Object.defineProperties(task, {
            'text': { value: 'New Task' }
        });
        var descriptor = Object.getOwnPropertyDescriptor(task, 'text');
        console.log(descriptor);
        */
    </script>
</body>
</html>